﻿using UnityEngine;
using System.Collections;
namespace Room
{
    public class RCAIPrison : RCAIBase
    {

        private static Vector3 PRISON_POS = new Vector3(58.23f, -2, 10.7f);
        private const float PRISON_ANGLE = -45f;
        private const float PRISON_WIDTH = 11f;
        private const float PRISON_HEIGHT = 16f;

        private const string PRISON_BLOCK_RES = "prison_door";
        private static Vector3 PRISON_BLOCK_POS = new Vector3(54.5f, -3, 6.8f);
        private static Vector3 PRISON_BLOCK_POS_UP = new Vector3(54.5f, 7, 6.8f);
        private const float PRISON_BLOCK_ANGLE = 45f;

        private const string PRISON_DOOR_NAME = "castle_obj_603b";
        private static Vector3 PRISON_DOOR_POS=new Vector3(-33.525f,-2.53f,-48.74f);
        private static Vector3 PRISON_DOOR_POS_UP = new Vector3(-33.525f, 0.475f, -48.74f);

        private System.DateTime redeemTime;
        private UI.Timer timer;
        private TweenAction tweenComponent;

        public RCAIPrison(RoomCharactor owner)
            : base(owner)
        {
            type = AIType.Prison;
        }

        public override void DoStart()
        {
            owner.charactorManager.CreateAirWall(PRISON_BLOCK_RES, PRISON_BLOCK_POS.x, PRISON_BLOCK_POS.y, PRISON_BLOCK_POS.z, PRISON_BLOCK_ANGLE, PRISON_BLOCK_RES);

            owner.transform.position = PRISON_POS;
            owner.transform.localEulerAngles = new Vector3(0, PRISON_ANGLE, 0);

            if (StaticData.isPlayerNeedJail)
            {
                var self = owner.charactorManager.GetSelfCharactor();
                if (!IsInPrisonRange(self.transform.position))
                    owner.StartCoroutine(GoToPrison());
            }
            else
            {
            }

            timer = owner.gameObject.AddComponent<UI.Timer>();

            var curTime = ServerTimerTool.CurrentUTCTime;
            var targetTime = ServerTimerTool.Java2CSTime(StaticData.playerData.redeemTime);
            var span = targetTime - curTime;
            
            timer.onTimerSecondChange += OnTimerSecondChange;
            timer.targetTime = Time.unscaledTime + (float)span.TotalSeconds;

            //监狱门口

            tweenComponent = owner.gameObject.AddComponent<TweenAction>();
        }

        public void RefreshTargetTime()
        {
            var curTime = ServerTimerTool.CurrentUTCTime;
            var targetTime = ServerTimerTool.Java2CSTime(StaticData.playerData.redeemTime);
            var span = targetTime - curTime;

            timer.onTimerSecondChange += OnTimerSecondChange;
            timer.targetTime = Time.unscaledTime + (float)span.TotalSeconds;
        }

        public override void Tick()
        {
            
            if (StaticData.isPlayerNeedJail)
            {

            }
            else
            {
                var self = owner.charactorManager.GetSelfCharactor();
                if (self!=null&& IsInPrisonRange(self.transform.position))
                {
                    var block = owner.charactorManager.GetCharactor(PRISON_BLOCK_RES);
                    if(block.transform.position == PRISON_BLOCK_POS)
                    {
                        TweenActionBase a = new TweenActionMove(block.transform, PRISON_BLOCK_POS_UP, 1, true);
                        tweenComponent.RunAction(a);

                        var door = GameObject.Find(PRISON_DOOR_NAME);
                        if(door!=null)
                        {
                            TweenActionBase a1 = new TweenActionMove(door.transform, PRISON_DOOR_POS_UP, 1, true);
                            tweenComponent.RunAction(a1);
                        }
                    }
                }
                else
                {
                    var block = owner.charactorManager.GetCharactor(PRISON_BLOCK_RES);
                    if (block.transform.position == PRISON_BLOCK_POS_UP)
                    {
                        TweenActionBase a = new TweenActionMove(block.transform, PRISON_BLOCK_POS, 1, true);
                        tweenComponent.RunAction(a);

                        var door = GameObject.Find(PRISON_DOOR_NAME);
                        if (door != null)
                        {
                            TweenActionBase a1 = new TweenActionMove(door.transform, PRISON_DOOR_POS, 1, true);
                            tweenComponent.RunAction(a1);
                        }
                    }
                }
            }
            var enumrator = owner.charactorManager.GetCharactors().GetEnumerator();
            while(enumrator.MoveNext())
            {
                var charactor = enumrator.Current.Value;
                if(charactor.type==CharactorType.OtherPlayer&&charactor.GetInView())
                {
                    if(IsInPrisonRange(charactor.transform.position))
                    {
                        (charactor.move as RCMoveNavigation).usingNavAgent = false;
                    }
                    else
                    {
                        (charactor.move as RCMoveNavigation).usingNavAgent = true;
                    }
                }
            }
        }

        private bool IsInPrisonRange(Vector3 position)
        {
            var localPositon = owner.transform.InverseTransformPoint(position);
            if (localPositon.x > -PRISON_WIDTH * 0.5f 
                && localPositon.x < PRISON_WIDTH * 0.5f
                && localPositon.z > -PRISON_HEIGHT * 0.5f
                && localPositon.z < PRISON_HEIGHT * 0.5f)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void OnTimerSecondChange(int lastSecond)
        {
            if(lastSecond==0)
            {
                var self = owner.charactorManager.GetSelfCharactor();
                self.SetKillNum(0);
                StaticData.isPlayerNeedJail = false;
                DataMessageTool.AddMessage(DataMessage.PLAYER_PRISON_REFRESH, null);
            }
        }

        private IEnumerator GoToPrison()
        {
            yield return null;
            //BoxManager.CreatePopupTextBox("你被抓了~(～￣▽￣)～");
            //yield return new WaitForSeconds(1f);
            var town = RoomManager.currentRoom as RoomTown;

            ManagerController.Instance.TrySceneTransfer(ServerScene.Town, owner.transform.position, "",()=> 
            {
                town.netManager.SendCSCurrentPosition(null);
                if (StaticData.beSend)
                    UI.PageManager.Instance.OpenPage("PrisonPage", "type=3");
                else
                    UI.PageManager.Instance.OpenPage("PrisonPage", "type=2");
            });

            /*
            town.netManager.TransferScene(ServerScene.Town, owner.transform.position,
                () =>
                {
                    town.netManager.SendCSCurrentPosition(null);
                    if(StaticData.beSend)
                        UI.PageManager.Instance.OpenPage("PrisonPage", "type=3");
                    else
                        UI.PageManager.Instance.OpenPage("PrisonPage", "type=2");
                });
                */
        }
    }

}

